package test4;
//把字符串转换成整数
//https://www.nowcoder.com/practice/1277c681251b4372bdef344468e4f26e?tpId=13&&tqId=11202&rp=6&ru=/activity/oj&qru=/ta/coding-interviews/question-ranking
//ASIC码：数字0是48，char型比int型数字大48
public class Solution {
    public int StrToInt(String str) {
        char[] ch=str.toCharArray();
        int len=str.length();
        if(len==0)return 0;
        int ans=0,m=0;
        for(int i=len-1;i>=0;i--){
            //如果非数字和符号，直接return 0
            if((ch[i]<48||ch[i]>48+9)&&ch[i]!='+'&&ch[i]!='-')return 0;
            //如果是数字，就从低位到高位累加，每次扩大10倍
            if(ch[i]>=48&&ch[i]<=48+9)ans+=(ch[i]-48)*Math.pow(10,m++);
            //System.out.println(ans);
        }
        if(ch[0]=='-')return -ans;//判断符号位
        return ans;
    }
}
